﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SilverPOP.Data.Process.Entity;
using SilverPOP.Data.Process.DAL;
using Utilities;
using Helpers;
using System.Data;

namespace SilverPOP.Data.Process.BLL
{
    public class DataSyncBLL : BaseBLL
    {

        public static DataTable GetAllNonResponderDataTable(long campaignID, String errorFileNameWithPath)
        {

            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@campaignId", campaignID));
            System.Data.DataTable dataTable = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataTable("SPR_SilverPOP_Populate", spParamValue);
     
            return dataTable;
         
        }

       
          
        private static String GetCMSNavigateText(String cmsSectionName)
        {
            return  (  ( ConfigHelpers.CampaignOwnerName != null && ConfigHelpers.CampaignOwnerName == "BARNARD" ) ? String.Empty : cmsSectionName );
        }


        public static List<String> GetSegmentList()
        {
            List<String> segmentList = new List<String>();   
            List<DataReaderObject> dataList= DAL.DAL.ExecuteStoredProcedureAndGetDataObject("SPR_Get_All_Segment_List"); 

                if (dataList!=null)
                {
                    foreach (DataReaderObject dataReader in dataList)
                    {
                        String value = ParsingHelpers.CleanNullOrEmptyValue(dataReader.ColumnValueList[0].ToString());
                        segmentList.Add(value);
                    }

                }

                if (segmentList != null && segmentList.Count > 0)
                {
                    return segmentList;
                }
            
              throw new Exception("Segment not found.");
        }

       


 

        public static Dictionary<long, String> GetActiveCampaingsID()
        {
            Dictionary<long, String> campaignIdNameList = new Dictionary<long, String>();
            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureAndGetDataObject("SPR_Get_Active_CampaignIDs");

            if (dataList!=null)
            {
                foreach (DataReaderObject dataReader in dataList)
                {
                    campaignIdNameList.Add((long)dataReader.ColumnValueList[0], dataReader.ColumnValueList[1].ToString());
                }
            }

            if (campaignIdNameList != null && campaignIdNameList.Count > 0)
            {
                return campaignIdNameList;
            }

            throw new Exception("There is no active campaign found.");
        }


        public static long GetCurrentActiveBlastID(long campaignId)
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@campaignId", campaignId));
            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Current_Active_Blast_Id", spParamValue);

            if (dataList != null)
            {
                foreach (DataReaderObject dataReader in dataList)
                {
                    return (long) dataReader.ColumnValueList[0];
                }
            }

            return 0;
        }

        public static bool UpdateBlasSegmentWithContactListID(long contactListId, long blastId, long segmentId)
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@blastId", blastId));
            spParamValue.Add(new ParamWithValue("@segmentId", segmentId));
            spParamValue.Add(new ParamWithValue("@contactListId", contactListId));
            return DAL.DAL.UpdateOrDeleteOrInsertDataUsingSP("SPR_Update_ContactList_Id", spParamValue);
        }

        public static List<long> GetActiveContactListByBlastID(long blastId)
        {
            List<long> contactList = new List<long>();

            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@BlastId", blastId));
            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Active_ContactList_By_Blast_Id", spParamValue);

            if (dataList != null)
            {
               

                foreach (DataReaderObject dataReader in dataList)
                {
                    if (dataReader.ColumnValueList[0] != null && !String.IsNullOrEmpty(dataReader.ColumnValueList[0].ToString()))
                    {
                        contactList.Add((long)dataReader.ColumnValueList[0]);
                    }
                }
            }

            return (contactList != null && contactList.Count > 0) ? contactList  :null;

        }
    
    }//end class
}
